package com.situ.mall.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/page")
public class PageController {

    /**
     * /page/user/add     /page/login
     *
     * @return
     */
    @RequestMapping("/**")
    public String path(HttpServletRequest request, Model model) {
        String requestURI = request.getRequestURI();
        System.out.println("requestURI: " + requestURI);

        String queryString = request.getQueryString();
        String returnUri = queryString == null ? requestURI : requestURI + "?" + queryString;
        model.addAttribute("returnUri", returnUri);

        String[] paths = requestURI.split("/");
        //["","page","user","list"]
        //["","page","login"]
        if (paths.length == 4) {
            return paths[2] + "_" + paths[3];
        } else if (paths.length == 3) {
            return paths[2];
        } else {
            return "index";
        }
    }
}
